
import React, { Component } from 'react';
import { createPage, base } from 'nc-lightapp-front';
import { initTemplate } from './init';
import RefChoiceBox from './component/ftDetailComp';
import {
AREA,
PAGECODE,
BUTTONID,
MULTILANG,
DATASOURCECACHE,
UISTATUS,
CARDTABLEAREAIDS,
CARDTABLEAREAIDS2,
CARDTABLEAREANAMES,
} from '../constance';
import { buttonClick, lineBtnClick } from './btnClicks';
import { getCardDisableHotKeyBtn } from '../tool/hotKeysUtil';
import { onRowsSelected } from './viewControl/buttonControl';
import pageInfoClick from './btnClicks/pageInfoClick';
import { getAreaAndBtnId } from './utils/cardPageUtil';
import { initLang, getLangByResId } from '../tool/multiLangUtil';
import { createCardTitle } from '../tool/titleUtil.js';
import { headAfterEvent, bodyAfterEvent } from './afterEvents';
import { headBeforeEvent, bodyBeforeEvent } from './beforeEvents';

const { NCAffix, NCAnchor, NCScrollElement, NCScrollLink, NCDiv } = base;

const formId = AREA.cardFormId;

class DiscountCard extends Component {
        constructor(props) {
                super(props);
                props.use.form(formId);
                this.state = {
                        copyRowDatas: null, // 复制行数据
                        isShowRefChoiceBox:false
                };
                this.contexts = null;
                // 所有复制行数据
                this.copyLineDatas = {
                                        [AREA.cardBody_1]: null,
                        [AREA.cardBody_2]: null
                };
                this.bodyActionFlag = true; //操作列按钮可用标志
                initLang(this, MULTILANG.langfileId, MULTILANG.domainName, initTemplate.bind(this, this.props));
        }

        componentWillMount() {
                // 关闭浏览器
                window.onbeforeunload = () => {
                        let status = this.props.cardTable.getStatus(AREA.cardBody_1);
                        if (status == UISTATUS.edit) {
                                return getLangByResId(this, '4001PUBMESSAGE-0000003'); /* 国际化处理： 当前单据未保存，您确认离开此页面*/
                        }
                };
        }

        // 获取列表肩部信息
        getTableHead = moduleId => {
                let { bodyAreaId } = getAreaAndBtnId(moduleId); // 肩部按钮区域
                return (
                        <div className="shoulder-definition-area">
                                <div className="definition-icons">
                                        {this.props.button.createButtonApp({
                                                area: bodyAreaId,
                                                ignoreHotkeyCode: getCardDisableHotKeyBtn(),
                                                onButtonClick: lineBtnClick.bind(this, moduleId),
                                        })}
                                </div>
                        </div>
                );
        };

        // 楼层点击回调函数
        clickFunction = moduleId => {
                this.props.cardTable.toggleCardTable(moduleId, true);
        };

        render() {
                let { button, BillHeadInfo, cardTable, form, cardPagination } = this.props;
                const { createCardPagination } = cardPagination;
                let { createForm } = form;
                let { createCardTable } = cardTable;
                const { createBillHeadInfo } = BillHeadInfo;
                const { createButtonApp } = button;

                return (
                        <div className="nc-bill-extCard" id="scm-ct-purdaily-card">
                                <div className="nc-bill-top-area">
                                        <RefChoiceBox isShowRefChoiceBox={this.state.isShowRefChoiceBox}
                                        confirm={(refvalue)=>{
                                                this.setState({isShowRefChoiceBox:false})
                                        }}
                                         ht={billTableArea2.call(this, createCardTable)}
                                        />
                                        <NCAffix>
                                                <NCDiv areaCode={NCDiv.config.HEADER} className="nc-bill-header-area">
                                                        {headerTitleArea.call(this, createBillHeadInfo)}
                                                        {headerButtonArea.call(this, createButtonApp)}
                                                        {headerCardPaginationArea.call(this, createCardPagination)}
                                                </NCDiv>
                                        </NCAffix>
                                        {floorArea.call(this)}
                                        {billFromArea.call(this, createForm)}
                                </div>
                                <div className="nc-bill-bottom-area">{billTableArea.call(this, createCardTable)}</div>
                        </div>
                );
        }
}

// 标题区
function headerTitleArea() {
        return (
                <div className="header-title-search-area">
                        {createCardTitle(this, {
                                billCode: '', // 单据号
                                backBtnClick: buttonClick.bind(this, this.props, BUTTONID.Back), // 返回按钮的点击事件
                        })}
                </div>
        );
}

// 按钮区
function headerButtonArea(createButtonApp) {
        return (
                <div className="header-button-area">
                        {createButtonApp({
                                area: AREA.cardHead,
                                onButtonClick: buttonClick.bind(this),
                        })}
                </div>
        );
}

// 翻页区
function headerCardPaginationArea(createCardPagination) {
        return (
                <div className="header-cardPagination-area">
                        {createCardPagination({
                                dataSource: DATASOURCECACHE.dataSourceListCacheKey,
                                handlePageInfoChange: pageInfoClick.bind(this),
                        })}
                </div>
        );
}

// 楼层区
function floorArea() {
        return (
                <div className="nc-bill-table-area">
                        <NCAnchor>
                                <NCScrollLink to={AREA.cardFormId} spy={true} smooth={true} duration={300} offset={-100}>
                                        <p>{getLangByResId(this, '4001PUBMESSAGE-0000005')}</p>
                                        {/* 国际化处理： 基本信息*/}
                                </NCScrollLink>
                                {CARDTABLEAREAIDS.map((moduleId, index) => {
                                        return (
                                                <NCScrollLink
                                                        to={moduleId}
                                                        spy={true}
                                                        smooth={true}
                                                        duration={300}
                                                        offset={-100}
                                                        clickFun={this.clickFunction.bind(this, moduleId)}
                                                >
                                                        <p>{getLangByResId(this, CARDTABLEAREANAMES[index])}</p>
                                                </NCScrollLink>
                                        );
                                })}
                        </NCAnchor>
                </div>
        );
}

// 表单区
function billFromArea(createForm) {
        return (
                <div className="nc-bill-form-area">
                        <NCScrollElement name={AREA.cardFormId}>
                                {createForm(AREA.cardFormId, {
                                        onBeforeEvent: headBeforeEvent.bind(this),
                                        onAfterEvent: headAfterEvent.bind(this),
                                })}
                        </NCScrollElement>
                </div>
        );
}

// 表格区
function billTableArea(createCardTable) {
        let i=0;
        return (
                <div className="nc-bill-table-area">
                          <NCScrollElement name={CARDTABLEAREAIDS[0]}>
                                                {createCardTable(CARDTABLEAREAIDS[0], {
                                                        tableHead: this.getTableHead.bind(this, CARDTABLEAREAIDS[0]),
                                                        hideAdd: true,
                                                        showCheck: true,
                                                        // adaptionHeight: true,
                                                        hideSwitch: () => false,
                                                        hideDel: !this.bodyActionFlag,
                                                        hideModelSave: true,
                                                        selectedChange: onRowsSelected.bind(this),
                                                        onBeforeEvent: bodyBeforeEvent.bind(this),
                                                        onAfterEvent: bodyAfterEvent.bind(this),
                                                })}
                                        </NCScrollElement>
                </div>
        );
}

// 表格区
function billTableArea2(createCardTable) {
        return (
                <div className="nc-bill-table-area">
                         <NCScrollElement name={CARDTABLEAREAIDS[1]}>
                                                {createCardTable(CARDTABLEAREAIDS[1], {
                                                        tableHead: this.getTableHead.bind(this, CARDTABLEAREAIDS[1]),
                                                        hideAdd: true,
                                                        showCheck: true,
                                                        // adaptionHeight: true,
                                                        hideSwitch: () => false,
                                                        hideDel: !this.bodyActionFlag,
                                                        hideModelSave: true,
                                                        selectedChange: onRowsSelected.bind(this),
                                                        onBeforeEvent: bodyBeforeEvent.bind(this),
                                                        onAfterEvent: bodyAfterEvent.bind(this),
                                                })}
                                        </NCScrollElement>
                </div>
        );
}

DiscountCard = createPage({
        orderOfHotKey: [AREA.cardFormId,...CARDTABLEAREAIDS],
        billinfo: {
                billtype: 'extcard',
                pagecode: PAGECODE.cardPagecode,
                headcode: AREA.cardFormId,
                bodycode: CARDTABLEAREAIDS.map(item =>({ [item]: 'cardTable' }))
    }
})(DiscountCard);

export default DiscountCard;
